package com.safe.ly_safe.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.safe.ly_safe.R;
import com.safe.ly_safe.base.BaseActivity;
import com.safe.ly_safe.control.view.MainView;
import com.safe.ly_safe.utils.StatusBarUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @PACKAGE :com.safe.ly_safe.activity
 * @Author : HSF
 * @Time : On 2022/4/25 14:39
 * @Description : SetUpActivity
 * 登陆页--服务器列表
 */
public class ServerListActivity extends BaseActivity implements MainView {

    @BindView(R.id.backIv)
    ImageView backIv;

    @BindView(R.id.addIv)
    ImageView addIv;

    @BindView(R.id.titleTv)
    TextView titleTv;

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    private BaseQuickAdapter mAdapter;

    @Override
    protected void setFullScreen() {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_set_layout;
    }

    @Override
    protected String loadingMsg() {
        return "加载中...";
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarLightMode(getWindow()); //设置状态栏黑色字体图标
        titleTv.setText("服务器列表");
        addIv.setVisibility(View.VISIBLE);
        setLisInfo();
    }

    @Override
    public void showLoading(int type) {

    }

    @Override
    public void hideLoading(int type) {

    }

    @Override
    public void onSuccess(int type, Object result) {

    }

    @Override
    public void onError(int type, Throwable e) {

    }

    private void setLisInfo(){
        List<String> nameLis = new ArrayList<>();
        nameLis.add("156.23.996.32");
        nameLis.add("166.26.75.66.356");
        nameLis.add("内网SDP");
        nameLis.add("144.138.26.885.25");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.set_up_item_layout,nameLis) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                TextView mTv = helper.getView(R.id.nameTv);
                mTv.setText(item);
            }
        };
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setResult(RESULT_OK,new Intent().putExtra("serverUrl",nameLis.get(position)));
                finish();
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.backIv,R.id.addIv})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.backIv:
                finish();
                break;
            case R.id.addIv:
                //添加
                startActivityForResult(new Intent(this,ServerSetActivity.class),1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            if(requestCode == 1 && resultCode == RESULT_OK){
//                String serverUrl = data.getStringExtra("serverUrl");
//                Toast.makeText(this,"serverUrl::"+serverUrl,Toast.LENGTH_SHORT).show();
            }
        }
    }
}
